﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Repositories;
using DomenModel;
using System.Data.Entity;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;

namespace Infrastructure.Repositories
{
    public class StatusRepo : ICrud<Status>
    {

        /// <summary>
        /// Gets the Status by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
          public Status GetById(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                Status statusById = _db.Statuses.FirstOrDefault(p => p.StatusId == Id);
                return statusById;
            }

        }

          /// <summary>
          /// Gets the Status by title.
          /// </summary>
          /// <param name="Name">The name.</param>
          /// <returns></returns>
          public Status GetByTitle(string Name)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                Status statusByTitle = _db.Statuses.FirstOrDefault(p => p.Title == Name);
                return statusByTitle;
            }

        }


          /// <summary>
          /// Saves the Status to database.
          /// </summary>
          /// <param name="status">The status.</param>
          public void Save(Status status)
          {
              using (GeneralContext _db = new GeneralContext())
              {
                  _db.Statuses.Add(new Status
                  {

                      Title = status.Title,
                      Value = status.Value,
                      Discount = status.Discount,

                  });
                  _db.SaveChanges();
              }
          }

          /// <summary>
          /// Updates the Status.
          /// </summary>
          /// <param name="statusUpdate">The status update.</param>
          public void Update(Status statusUpdate)
          {
              using (GeneralContext _db = new GeneralContext())
              {
                  Status statusById = _db.Statuses.First(p => p.StatusId == statusUpdate.StatusId);
                  statusById.Title = statusUpdate.Title;
                  statusById.Value = statusUpdate.Value;
                  statusById.Discount = statusUpdate.Discount;
                  _db.SaveChanges();
              }
          }

          /// <summary>
          /// Deletes the Status from database.
          /// </summary>
          /// <param name="Id">The id.</param>
          public void Delete(long Id)
          {
              using (GeneralContext _db = new GeneralContext())
              {
                  Status statusById = _db.Statuses.First(p => p.StatusId == Id);
                  _db.Statuses.Remove(statusById);
                  _db.SaveChanges();
              }
          }

          /// <summary>
          /// Gets the collection of Status.
          /// </summary>
          /// <returns></returns>
          public List<Status> GetCollection()
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var listOfStatuses = _db.Statuses.ToList();
                return listOfStatuses;
            }
        } 
    }
}
